<?php
/**
 * Created by: i@fedax.cn
 * Date: 2018/7/16 22:40
 */

namespace app\admin\controller;

use app\admin\model\SendMsg;
use app\admin\model\User;

class UPush extends Index
{



    //推送记录
    public function historyIndex()
    {
        return view();
    }
    public function historyIndexData()
    {
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit',10),
        );
        $data = SendMsg::order('create_time DESC')->paginate($page_config);
        return json($data);
    }

    //友盟推送
    public function index()
    {
        if($_POST){
            $type = input('type');  //1：全部，2：苹果，3：安卓
            $content = input('content');
            $re = sendUPushBroadcast($content, '', $type);

            if ($re) $this->success('发送成功');
            else $this->error('发送失败');

        }else
            return view();
    }

    public function sendMsgIndex()
    {
        return view();
    }
    //绯闻小助手推送全服消息(网易推送)
    public function sendMsg()
    {
        $msg = input('msg');
        set_time_limit (0);
        if(!$msg) return ['code'=>-100,'msg'=>'消息不能为空','url'=>'/SendMsgIndex'];
        $msg = ['msg'=>$msg];
        $map = [];
        $map[] = ['user_type','eq',1];
        $map[] = ['last_login','egt',time()-7 * 24 * 3600];

        $ids = User::where($map)->column('id');
        if(!$ids) return ['code'=>-100,'msg'=>'最近7天无人登录','url'=>'/SendMsgIndex'];

        $i = 0;
        $send_users = [];
        $send_users[$i] = [];
        foreach($ids as $v){
            if(count($send_users[$i]) == 500){
                $i ++;
                $send_users[$i] = [];
            }
            $send_users[$i][] = $v;

        }

        shuffle($send_users);

        $WyYxModel = new \think\wyyxapi\WyYxAPI();
        foreach($send_users as $key=>$value){
            $res = $WyYxModel->sendNotice($value,$msg);
            if ($res['code'] != 200) return ['code'=>-100,'smg'=>'发送失败!','url'=>'/SendMsgIndex'];
        }
        return ['code'=>100,'smg'=>'发送成功!','url'=>'/SendMsgIndex'];

    }
}